<?php
/* 
	[BuyPg] (C) 2010-2011 Buypg
	$Id: NbSiteModel.class.php 2011-4-13 12:56:41 wufujun $
 */

if (!defined('THINK_PATH')){
	exit('Access Denied');
}

class NbSiteModel extends Model{
	//自动验证
	protected $_validate = array(
		array('site_name','require','网站名称不能为空',0),
		array('site_link','require','网址不能为空',0),
		array('class_id','require','请选择分类',0),
	);
	
	//自动填充
	protected $_auto = array(
		array('site_key' , 'getsitekey' , 3 , 'callback'),
		array('site_link' , 'urlencode' , 3 , 'function'),
		array('site_date' , 'getYmdToStr' ,1, 'function'),
		array('site_desc' , 'delehtml' , 3 , 'callback'),
		array('user_id' , 'getUid' , 1 , 'callback'),
	);
	
	//生成key
	protected function getsitekey(){
		if($_POST['site_link']){
			$arrLink = parse_url($_POST['site_link']);
			$sitekey = sha1($arrLink['host']);
		}
		return $sitekey;
	}
	
	//获取UID
	protected function getUid(){
		global $_SGLOBAL;
		return $_SGLOBAL['G_uid'];
	}
	
	//清除html标签
	protected function delehtml($str){
		import("@.ORG.Input");
		return Input::deleteHtmlTags($str);
	}

	//获取推荐site
	public function getCommendSite($where,$order,$limit){
		$result = $this->where($where)->order($order)->limit($limit)->select();
		foreach ($result as $key=>$value){
			$result[$key]['full_site_name'] = $value['site_name'];
			$result[$key]['site_name'] = msubstr($value['site_name'],0 ,5 ,'utf-8' , FALSE);
		}
		return $result;
	}

	//获取带Limit的site 注意：用此方法只取一条数据时（limi 1），返回值同样为二维数组，如有需要请在Action控制器里把数组重新构造成一维数组使用。
	public function  getLimitSite($where,$order,$limit){
		$DbClassFen = D('ClassF'); //实例化class_f
		$result = $this->where($where)->order($order)->limit($limit)->select();
		if($result){
			foreach ($result as $key => $value) {
				$result[$key]['class_fen'] = $DbClassFen->getClassF($value['class_id']); //获取当前site所在class下的最高、平均、最低分。
				//site f1-f7分数格式化
				for ($i = 1; $i < 8; $i++){
					$result[$key]['f'.$i.'_imgheight'] = $value['f'.$i];
					$result[$key]['f'.$i] = formatFen($value['f'.$i]);
				}
				$result[$key]['site_link'] = urldecode($value['site_link']);
			}
		}
		return $result;
	}

	//获取site
	public function  getSite($where){
		$DbClassFen = D('ClassF'); //实例化class_f
		$result = $this->where($where)->find();
		if($result){
			//site f1-f7分数格式化
			for ($i = 1; $i < 8; $i++){
				$result['f'.$i.'_imgheight'] = $result['f'.$i];
				$result['f'.$i] = formatFen($result['f'.$i]);
			}
			$result['class_fen'] = $DbClassFen->getClassF($result['class_id']); //获取当前site所在class下的最高、平均、最低分。
			$result['site_link'] = urldecode($result['site_link']);
		}
		return $result;
	}

}
?>
